import * as actionType from './actionType'
import { warningListApi, warningAddApi, warningUpdateApi } from '../../http/afterLoanApis'

export const SetSpinningAction = (spinning) => {
    return ({
        type: actionType.SET_SPINNING,
        spinning: spinning
    })
}

export const SetPageSizeAction = (pageSize) => {
    return ({
        type: actionType.SET_PAGESIZE,
        pageSize
    })
}

export const SetCurrentPageAction = (currentPage) => {
    return ({
        type: actionType.SET_CURRENTPAGE,
        currentPage
    })
}

// 警告列表 action
export const WarningListAction = (warningList) => ({
    type: actionType.WARNING_LIST,
    warningList
})

// 警告列表 异步
export const WarningListAsyncAction = (pageInfo) => (dispatch) =>
    warningListApi(pageInfo)
        .then(res => {
            if (res.data.code === 200) {
                dispatch(WarningListAction({
                    warningList: res.data.data,
                    count: res.data.count
                }))
                console.log(res);
                return Promise.resolve({
                    count: res.data.count,
                    msg: res.data.msg
                })
            } else {
                return Promise.reject(res.data)
            }
        })
        .catch(err => {
            return Promise.reject(err)
        })

// 警告新增 action
export const WarningAddAction = () => ({
    type: actionType.WARNING_ADD
})

// 警告新增 async
export const WarningAddAsyncAction = (values) => (dispatch) =>
    warningAddApi(values)
        .then(res => {
            if (res.data.code === 200) {
                dispatch(WarningAddAction(res.data.data))
                return Promise.resolve('添加成功！')
            } else {
                return Promise.reject(res.data)
            }
        })
        .catch(err => {
            return Promise.reject(err)
        })

// 警告修改 action
export const WarningUpdateAction = () => ({
    type: actionType.WARNING_UPDATE
})

// 警告修改 async
export const WarningUpdateAsyncAction = (values) => (dispatch) =>
    warningUpdateApi(values)
        .then(res => {
            if (res.data.code === 200) {
                dispatch(WarningUpdateAction())
                return Promise.resolve('修改成功！')
            } else {
                return Promise.reject(res.data)
            }
        })
        .catch(err => {
            return Promise.reject(err)
        })